package com.fsc.comadapoc1.presentation.panels
{
	import com.fsc.comadapoc1.domain.PortfolioModel;
	import com.fsc.comadapoc1.entities.LiquidityReportRequest;
	import com.fsc.comadapoc1.events.LoadLiquidityExitFeesEvent;
	import com.fsc.comadapoc1.events.LoadLiquidityReportChartEvent;
	import com.fsc.comadapoc1.events.LoadLiquidityReportEvent;
	import com.fsc.comadapoc1.events.LoadPortfolioEvent;
	import com.fsc.comadapoc1.events.PanelChangeEvent;
	
	import flash.events.EventDispatcher;

	
	[Event(name="loadLiquidityReport", type="com.fsc.comadapoc1.events.LoadLiquidityReportEvent")]
	[ManagedEvents(names="loadLiquidityReport,loadLiquidityReportChart")]
	public class LiquidityManagementPanelPM extends EventDispatcher
	{
		[Bindable]
		[Inject]
		public var model:PortfolioModel;
		
		/** The landing state **/
		public const LANDING_STATE : String = "landingState";
		
		/** The searching state. **/
		public const SEARCHING_STATE : String = "searchingState";
		
		/** The results state. **/
		public const RESULTS_STATE : String = "resultsState";
		
		/** The no results state. **/
		public const NO_RESULTS_STATE : String = "noResultsState";
		
		private var _currentState:String = LANDING_STATE;
		
		/**
		 * Get the current state of the presentation model.
		 */
		[Bindable]
		public function get currentState() : String
		{
			return _currentState;
		}
		
		/**
		 * Sets the current state of the presentation model.
		 */
		public function set currentState( state:String ) : void
		{
			_currentState = state;	
		}
		
		[Bindable]
		public var formReady:Boolean;
		
		public function requestReport(effectiveDate:Date,referenceCurrency:String,exitFee:Number):void{
			var reportParams:LiquidityReportRequest = new LiquidityReportRequest();
			reportParams.asAt = effectiveDate;
			reportParams.exitFee = exitFee;
			reportParams.referenceCurrency = referenceCurrency
			
			dispatchEvent(new LoadLiquidityReportEvent(LoadLiquidityReportEvent.LOAD_LIQUIDITY_REPORT,reportParams));	
			dispatchEvent(new LoadLiquidityReportChartEvent(LoadLiquidityReportChartEvent.LOAD_LIQUIDITY_REPORT_CHART,reportParams));
		}
		
		
		[MessageHandler(selector="liquidityReportComplete")]
		public function onReportResults(event:LoadLiquidityReportEvent):void{
			checkResultsToDisplay();
		}
		
		
		[MessageHandler(selector="liquidityReportChartComplete")]
		public function onChartResults(event:LoadLiquidityReportChartEvent):void{
			checkResultsToDisplay();
		}
		
		protected function checkResultsToDisplay():void{
			if(model.currentPortfolio.liquidityReportChart.length > 0 || model.currentPortfolio.liquidityReport.length > 0){
				currentState = RESULTS_STATE;
				trace("lots of results")
			}else{
				currentState = NO_RESULTS_STATE;
				trace("no results" + model.currentPortfolio.liquidityReportChart.length + "xx" + model.currentPortfolio.liquidityReportChart.length)
			}
		}
		
		[MessageHandler]
		public function handleStateChange(event:PanelChangeEvent):void{
			if(event.state == PanelChangeEvent.PORTFOLIOS_VIEW_STATE){
				//	groupedData.source = pm.model.currentPortfolio.liquidityReport;
				//	groupedData.refresh();
				formReady = false;
				currentState = LANDING_STATE;
			}
		}			
	}
}